package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.DataVerification;
import com.ruoyi.system.domain.vo.SysLearningGardenVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysLearningGarden;
import com.ruoyi.system.service.ISysLearningGardenService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学习园地Controller
 *
 * @author ruoyi
 * @date 2022-03-26
 */
@RestController
@RequestMapping("/system/garden")
public class SysLearningGardenController extends BaseController
{
    @Autowired
    private ISysLearningGardenService sysLearningGardenService;

    /**
     * 查询学习园地列表
     * @PreAuthorize("@ss.hasPermi('system:garden:list')")
     */
    @GetMapping("/list")
    public TableDataInfo list(SysLearningGarden sysLearningGarden)
    {
        startPage();
        List<SysLearningGardenVo> list = sysLearningGardenService.selectSysLearningGardenList(sysLearningGarden);
        return getDataTable(list);
    }

    /**
     * 导出学习园地列表
     */
    @PreAuthorize("@ss.hasPermi('system:garden:export')")
    @Log(title = "学习园地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLearningGarden sysLearningGarden)
    {
        List<SysLearningGardenVo> list = sysLearningGardenService.selectSysLearningGardenList(sysLearningGarden);
        ExcelUtil<SysLearningGardenVo> util = new ExcelUtil<SysLearningGardenVo>(SysLearningGardenVo.class);
        util.exportExcel(response, list, "学习园地数据");
    }

    /**
     * 获取学习园地详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:garden:query')")
    @GetMapping(value = "/{learningGardenId}")
    public AjaxResult getInfo(@PathVariable("learningGardenId") Long learningGardenId)
    {
        SysLearningGardenVo sysLearningGarden = sysLearningGardenService.selectSysLearningGardenByLearningGardenId(learningGardenId);
        //浏览量＋1
        sysLearningGarden.setViews(sysLearningGarden.getViews() + 1);
        sysLearningGardenService.updateSysLearningGarden(sysLearningGarden);
        return AjaxResult.success(sysLearningGarden);
    }

    /**
     * 新增学习园地
     */
    @DataVerification(name = "添加学习园地")
    @PreAuthorize("@ss.hasPermi('system:garden:add')")
    @Log(title = "学习园地", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysLearningGarden sysLearningGarden)
    {
        return toAjax(sysLearningGardenService.insertSysLearningGarden(sysLearningGarden));
    }

    /**
     * 修改学习园地
     */
    @DataVerification(name = "修改学习园地")
    @PreAuthorize("@ss.hasPermi('system:garden:edit')")
    @Log(title = "学习园地", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysLearningGarden sysLearningGarden)
    {
        return toAjax(sysLearningGardenService.updateSysLearningGarden(sysLearningGarden));
    }

    /**
     * 删除学习园地
     */
    @PreAuthorize("@ss.hasPermi('system:garden:remove')")
    @Log(title = "学习园地", businessType = BusinessType.DELETE)
	@DeleteMapping("/{learningGardenIds}")
    public AjaxResult remove(@PathVariable Long[] learningGardenIds)
    {
        return toAjax(sysLearningGardenService.deleteSysLearningGardenByLearningGardenIds(learningGardenIds));
    }
}
